package cn.wolfcode.cms.web.controller;

import cn.wolfcode.cms.annotation.PermissionName;
import cn.wolfcode.cms.domain.Collect;
import cn.wolfcode.cms.query.QueryObject;
import cn.wolfcode.cms.service.ICollectService;
import cn.wolfcode.cms.util.JSONResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

//部门
@Controller
@RequestMapping("collect")
public class CollectController {

    @Autowired
    private ICollectService collectService;

    @RequiresPermissions("collect:index")
    @PermissionName("部门首页")
    @RequestMapping("")
    public String index() {
        collectService.list();
        return "collect/list1";
    }

    //部门信息列表/带分页
    @RequiresPermissions("collect:list")
    @PermissionName("部门列表")
    @RequestMapping("list")
    @ResponseBody
    public Object list(QueryObject qo) {
        return collectService.query(qo);
    }


    //所有部门信息数据/无分页
    @RequestMapping("listData")
    @ResponseBody
    public Object listData() {
        return collectService.list();
    }


    //部门添加
    @RequiresPermissions("collect:add")
    @PermissionName("部门添加")
    @RequestMapping("save")
    @ResponseBody
    public Object save(Collect entity) {
        JSONResult result = new JSONResult();
        try {
            collectService.save(entity);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("添加失败");
        }
        return result;
    }

    //部门编辑
    @RequiresPermissions("collect:edit")
    @PermissionName("部门编辑")
    @RequestMapping("edit")
    @ResponseBody
    public Object edit(Collect entity) {
        JSONResult result = new JSONResult();
        try {
            collectService.update(entity);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("编辑失败");
        }
        return result;
    }

    //部门删除
    @RequiresPermissions("collect:delete")
    @PermissionName("部门删除")
    @RequestMapping("delete")
    @ResponseBody
    public Object delete(Long id) {
        JSONResult result = new JSONResult();
        try {
            collectService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("删除失败");
        }
        return result;
    }

    //所有部门信息数据/无分页
    @RequestMapping("updateCollect")
    @ResponseBody
    public Object updateCollect(@RequestParam Map<String, String> map) {
        JSONResult result = new JSONResult();
        try {
            collectService.updateCollect(map);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("操作失败");
        }
        return result;
    }
}
